package net.jltec.api.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.jltec.core.utils.RequestProcessorUtils;
import net.jltec.core.utils.SpringContext;
import net.jltec.core.utils.Struts2Utils;
import net.jltec.core.utils.Tools;
import net.jltec.core.web.action.Struts2BaseAction;
import net.jltec.core.web.validate.Field;
import net.jltec.model.sys.service.ISystemService;
import net.jltec.model.util.RequestContext;
import net.jltec.model.workflow.dto.GoToNextDTO;
import net.jltec.model.workflow.dto.GoToNextForm;
import net.jltec.model.workflow.dto.VariableDTO;
import net.jltec.model.workflow.service.IWorkflowService;

import org.apache.commons.beanutils.ConvertUtils;

import com.alibaba.fastjson.JSON;

@SuppressWarnings("unchecked")
public class ApiAction extends Struts2BaseAction {
	private static final long serialVersionUID = 1327481629668821322L;
	private ISystemService service;
	public ISystemService getService() {
		return service;
	}

	public void setService(ISystemService service) {
		this.service = service;
	}
	
	
	public void showFlowByUser(){
		try {
			List<Field> fields = createFields();
			Map params = createParams(fields);
			setLoginIdToMap(params, "manid");
			processRowAndPage(params, ROW_USER, -4);
			//List list = this.service.queryForList(params, "WorkflowSp.sp_workflow_task_list");
			params.put("logonid", RequestContext.getAppLogonid());
			
			
			List list = this.getJservice().queryForList(params, "{call sp_workflow_task_list(#logonid:integer#,#xml:varchar#,#row:integer#,#page:integer#)} ");
			this.setPage(list);
			
			for(int i=0;i<list.size();i++){
				Map m = (Map) list.get(i);
				System.out.println(m.get("wiid")+","+m.get("rowcnt"));
			}
			
			Struts2Utils.renderJson(JSON.toJSONString(list));
		} catch (Exception e) {
			RequestProcessorUtils.processException(getRequest(), e);
		}
	}
	
	public void flowTaskByWinid(){
		try {
			List<Field> fields = createFields();
			Map params = createParams(fields);
			setLoginIdToMap(params, "manid");
			params.put("logonid", RequestContext.getAppLogonid());
			Integer winid = (Integer) ConvertUtils.convert(params.get("winid"),Integer.class);
			IWorkflowService workflowService = (IWorkflowService) SpringContext.getContext().getBean("workflowService");
			GoToNextForm form = workflowService.getGoToNextFormByWinid(winid);	
			Map json = new HashMap();
			json.put("winid", form.getWinid());
			json.put("varid", form.getVarid());
			List<VariableDTO> labs = form.getVariables();
			List vars = new ArrayList();
			 for(VariableDTO v : labs){
				 Map m = new HashMap();
				 m.put("value", v.getValue());
				 m.put("name", v.getLabel());
				 vars.add(m);
			 }
			 json.put("vars", vars);
			 Struts2Utils.renderJson(JSON.toJSONString(json));
		} catch (Exception e) {
			RequestProcessorUtils.processException(getRequest(), e);
		}
	}
	
	public void getoNext(){
		Map json = new HashMap();
		try {
			Integer winid = (Integer) ConvertUtils.convert(getParameter("winid","0"),Integer.class);
			Integer varid = (Integer) ConvertUtils.convert(getParameter("varid","0"),Integer.class);
			Double value = (Double) ConvertUtils.convert(getParameter("value","0"),Double.class);
			String note = getParameter("note","");
			
			Integer logonid = RequestContext.getAppLogonid();
			
			
			GoToNextDTO go = new GoToNextDTO(winid,varid,value,note);
			go.setLogonid(logonid);
			IWorkflowService workflowService = (IWorkflowService) SpringContext.getContext().getBean("workflowService");
			workflowService.gotoNext(go);
			
			
			
			json.put("message", "操作成功");
			
		} catch (Exception e) {
			RequestProcessorUtils.processException(getRequest(), e);
			json.put("message", "操作失败");
		}finally{
			RequestContext.end();
		}
		Struts2Utils.renderJson(JSON.toJSONString(json));
	}
	
	public void loginApp(){
		Map json = new HashMap();
		try {
			List<Field> fields = createFields();
			Map params = createParams(fields);
			//Tools.createXML(params);
			
			json = (Map) this.getJservice().query(params, "{call sp_sys_login_process_by_app(#username:varchar#,#password:varchar#)} ");
			if(json==null || json.isEmpty()){
				json = new HashMap();
				json.put("message", "登陆失败!");
			}
		} catch (Exception e) {
			RequestProcessorUtils.processException(getRequest(), e);
		} 
		Struts2Utils.renderJson(JSON.toJSONString(json));
	}
	
	public void getFlowByWiid(){
		Map json = new HashMap();
		try {
			
			List<Field> fields = createFields();
			Map params = createParams(fields);
			
			params.put("logonid", RequestContext.getAppLogonid());
			
			//Tools.createXML(params);
			json = (Map) this.getJservice().query(params, "{call sp_workflow_task_by_wiid(#logonid:integer#,#xml:varchar#)} ");
			
		} catch (Exception e) {
			RequestProcessorUtils.processException(getRequest(), e);
		}finally{
			RequestContext.end();
		}
		Struts2Utils.renderJson(JSON.toJSONString(json));
	}
	
	 
}
